package hrm.action.org;

import hrm.action.Action;
import hrm.action.ActionForward;
import hrm.dto.org.OrgCountDTO;
import hrm.dto.org.OrgViewDTO;
import hrm.serviceimple.OrgServiceImple;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class OrgMemberCount implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		ActionForward actfoward = new ActionForward();
		OrgServiceImple orgService = OrgServiceImple.getInstance();
		
		String orgcode = request.getParameter("code");
		String sdate = request.getParameter("sdate");
		orgcode = (orgcode==null)? "01000000":orgcode;
		String p = request.getParameter("p");
		p = (p==null) ? "1" : p;
		String orgofchief = (p.equals("1"))? "1":null ;
		if(sdate==null){
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH)+1;
			int date = cal.get(Calendar.DATE);
			sdate = (year+"-"+month+"-"+date);
		}

		String orgtree = orgService.getMenuTreeString(sdate).toString(); 
		OrgViewDTO vdto = new OrgViewDTO();
		vdto.setOrgcode(orgcode);
		vdto.setOrgofchief(orgofchief);
		OrgCountDTO countdto = orgService.getOrgCount(vdto);
		List<OrgViewDTO> orgperson = orgService.getOrgView(vdto);
		String orgname = orgService.getOrgbase(orgcode).getOrgname();
				
		request.setAttribute("code", orgcode);
		request.setAttribute("orgname", orgname);
		request.setAttribute("orgtree", orgtree);
		request.setAttribute("p", p);
		request.setAttribute("countdto", countdto);
		request.setAttribute("sdate", sdate);
		request.setAttribute("orgperson", orgperson);
		actfoward.setUrl("orgmembercount");

		return actfoward;
	}

}
